﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.NhomPhuongTienForm;
using ComputerVision.PhanLoaiChiTietPhuongTienForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmPhuongTienPCCC : frmDetail
    {
        private readonly IService<PhanLoaiChiTietPhuongTien> _phanLoaiChiTietPhuongTienService;
        private readonly IService<NhomPhuongTien> _nhomPhuongTienService;
        private readonly IService<PhuongTienCoSo> _phuongTienCoSoService;
        private readonly IUserService<aspnet_Users> _userService;
        public Guid IdPhieu;

        public frmPhuongTienPCCC()
        {
            InitializeComponent();
            _phanLoaiChiTietPhuongTienService = Program.Container.Resolve<IService<PhanLoaiChiTietPhuongTien>>();
            _nhomPhuongTienService = Program.Container.Resolve<IService<NhomPhuongTien>>();
            _phuongTienCoSoService = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm Phương tiện cơ sở";
            drlNhomPhuongTien.Focus();
            LoadNhomPhuongTien();
        }

        public frmPhuongTienPCCC(Guid id)
        {
            InitializeComponent();
            _phanLoaiChiTietPhuongTienService = Program.Container.Resolve<IService<PhanLoaiChiTietPhuongTien>>();
            _nhomPhuongTienService = Program.Container.Resolve<IService<NhomPhuongTien>>();
            _phuongTienCoSoService = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            drlNhomPhuongTien.Focus();
            LoadNhomPhuongTien();

            Text = @"Sửa Phương tiện cơ sở";

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _phuongTienCoSoService.Detail(id);
            if (item != null)
            {
                if (item.PhanLoaiChiTietPhuongTien != null &&
                    item.PhanLoaiChiTietPhuongTien.NhomPhuongTienId.HasValue)
                {
                    LoadPhanLoaiChiTiet(item.PhanLoaiChiTietPhuongTien.NhomPhuongTienId.Value);
                    
                    int i = 0;
                    foreach (NhomPhuongTien temp in drlNhomPhuongTien.Items)
                    {
                        if (temp.Id == item.PhanLoaiChiTietPhuongTien.NhomPhuongTienId)
                        {
                            drlNhomPhuongTien.SelectedIndex = i;
                            break;
                        }
                        i++;
                    }
                }

                txtTenPhuongTien.Text = item.Ten;
                txtSoLuong.Text = item.SoLuong.ToString();
                txtTTHD_Tot.Text = item.TinhTrangHoatDong_Tot.ToString();
                txtTTHD_TrungBinh.Text = item.TinhTrangHoatDong_TrungBinh.ToString();
                txtTTHD_HuHong.Text = item.TinhTrangHoatDong_HuHong.ToString();
                txtTTHD_Kem.Text = item.TinhTrangHoatDong_Kem.ToString();
                txtVitri.Text = item.Vitri;
                drlPhanLoaiChiTiet.SelectedValue = item.PhanLoaiChiTietPhuongTienId;
            }
        }

        private void btnAddPhanLoaiChitiet_Click(object sender, System.EventArgs e)
        {
            var frm = new frmPhanLoaiChiTietPhuongTienDetail(this);
            frm.ShowDialog();
        }

        private void btnAddNhomPhuongTien_Click(object sender, System.EventArgs e)
        {
            var frm = new frmNhomPhuongTienDetail(this);
            frm.ShowDialog();
        }

        public void LoadNhomPhuongTien()
        {
            drlNhomPhuongTien.DataSource = _nhomPhuongTienService.ToList().OrderBy(n => n.Ten).ToList();
            drlNhomPhuongTien.DisplayMember = "Ten";
            drlNhomPhuongTien.ValueMember = "Id";
        }

        public void LoadPhanLoaiChiTiet(Guid? idLoai)
        {
            drlPhanLoaiChiTiet.DataSource = _phanLoaiChiTietPhuongTienService.ToList().ToList().Where(a => a.NhomPhuongTienId.Equals(idLoai)).OrderBy(p => p.Ten).ToList();
            drlPhanLoaiChiTiet.DisplayMember = "Ten";
            drlPhanLoaiChiTiet.ValueMember = "Id";
        }

        public void LoadPhanLoaiChiTiet()
        {
            if (drlNhomPhuongTien.SelectedItem != null)
            {
                var nhom = drlNhomPhuongTien.SelectedItem as NhomPhuongTien;
                if (nhom != null)
                {
                    drlPhanLoaiChiTiet.DataSource = _phanLoaiChiTietPhuongTienService.ToList().ToList().Where(a => a.NhomPhuongTienId.Equals(nhom.Id)).OrderBy(p => p.Ten).ToList();
                    drlPhanLoaiChiTiet.DisplayMember = "Ten";
                    drlPhanLoaiChiTiet.ValueMember = "Id";
                }
            }
        }

        #region Validation

        protected override bool Validator()
        {
            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenPhuongTien.Text))
            {
                errorProvider.SetError(txtTenPhuongTien, "Bạn chưa nhập tên phương tiện. Xin vui lòng kiểm tra lại");
                return false;
            }

            if (drlPhanLoaiChiTiet.SelectedValue == null)
            {
                errorProvider.SetError(drlPhanLoaiChiTiet, "Bạn chưa chọn phân loại chi tiết. Xin vui lòng kiểm tra lại");
                return false;
            }

            return true;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new PhuongTienCoSo
                {
                    Id = Guid.NewGuid(),
                    Ten = txtTenPhuongTien.Text,
                    PhanLoaiChiTietPhuongTienId = Guid.Parse(drlPhanLoaiChiTiet.SelectedValue.ToString()),
                    PhieuDieuTraCoSoId = this.IdPhieu,
                    SoLuong = int.Parse(txtSoLuong.Text),
                    TinhTrangHoatDong_Tot = int.Parse(txtTTHD_Tot.Text),
                    TinhTrangHoatDong_TrungBinh = int.Parse(txtTTHD_TrungBinh.Text),
                    TinhTrangHoatDong_Kem = int.Parse(txtTTHD_Kem.Text),
                    TinhTrangHoatDong_HuHong = int.Parse(txtTTHD_HuHong.Text),
                    Vitri = txtVitri.Text,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _phuongTienCoSoService.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _phuongTienCoSoService.Detail(guid);
            if (item != null)
            {
                item.Ten = txtTenPhuongTien.Text;
                item.SoLuong = int.Parse(txtSoLuong.Text);
                item.TinhTrangHoatDong_Tot = int.Parse(txtTTHD_Tot.Text);
                item.TinhTrangHoatDong_TrungBinh = int.Parse(txtTTHD_TrungBinh.Text);
                item.TinhTrangHoatDong_Kem = int.Parse(txtTTHD_Kem.Text);
                item.TinhTrangHoatDong_HuHong = int.Parse(txtTTHD_HuHong.Text);
                item.Vitri = txtVitri.Text;
                item.PhanLoaiChiTietPhuongTienId = Guid.Parse(drlPhanLoaiChiTiet.SelectedValue.ToString());
                //item.PhieuDieuTraCoSoId = IdPhieu;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _phuongTienCoSoService.Update(item);
            }
        }

        #endregion

        private void drlNhomPhuongTien_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPhanLoaiChiTiet();
        }
    }

}
